﻿using System;
using AndroidSigner.Utils;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;

namespace AndroidSigner.ViewModels
{
    public class CreateKeyDialogViewModel : BindableBase, IDialogAware
    {
        public string Title { get; private set; }
        public event Action<IDialogResult> RequestClose;

        #region VM

        private string _keyAlias = string.Empty;

        public string KeyAlias
        {
            get => _keyAlias;
            set
            {
                _keyAlias = value;
                RaisePropertyChanged();
            }
        }

        private string _keyPassword = string.Empty;

        public string KeyPassword
        {
            get => _keyPassword;
            set
            {
                _keyPassword = value;
                RaisePropertyChanged();
            }
        }

        private string _keySurname = string.Empty;

        public string KeySurname
        {
            get => _keySurname;
            set
            {
                _keySurname = value;
                RaisePropertyChanged();
            }
        }

        private string _keyCompany = string.Empty;

        public string KeyCompany
        {
            get => _keyCompany;
            set
            {
                _keyCompany = value;
                RaisePropertyChanged();
            }
        }

        private string _keyDistrict = string.Empty;

        public string KeyDistrict
        {
            get => _keyDistrict;
            set
            {
                _keyDistrict = value;
                RaisePropertyChanged();
            }
        }

        private string _keyProvince = string.Empty;

        public string KeyProvince
        {
            get => _keyProvince;
            set
            {
                _keyProvince = value;
                RaisePropertyChanged();
            }
        }

        private string _keySavePath = string.Empty;

        public string KeySavePath
        {
            get => _keySavePath;
            set
            {
                _keySavePath = value;
                RaisePropertyChanged();
            }
        }

        #endregion

        #region DelegateCommand

        public DelegateCommand SaveKeyCommand { set; get; }
        public DelegateCommand GenerateKeyCommand { set; get; }

        #endregion

        public CreateKeyDialogViewModel(IDialogService dialogService)
        {
            SaveKeyCommand = new DelegateCommand(delegate
            {
                var fileDialog = new SaveFileDialog
                {
                    DefaultExt = ".jks",
                    Filter = "证书文件(*.jks)|*.jks",
                    RestoreDirectory = true
                };
                if (fileDialog.ShowDialog() == true)
                {
                    KeySavePath = fileDialog.FileName;
                }
            });
            GenerateKeyCommand = new DelegateCommand(delegate
            {
                if (string.IsNullOrWhiteSpace(_keyAlias) || string.IsNullOrWhiteSpace(_keyPassword) ||
                    string.IsNullOrWhiteSpace(_keySurname) || string.IsNullOrWhiteSpace(_keyCompany) ||
                    string.IsNullOrWhiteSpace(_keyDistrict) || string.IsNullOrWhiteSpace(_keyProvince)||
                    string.IsNullOrWhiteSpace(_keySavePath))
                {
                    dialogService.ShowDialog(
                        "AlertMessageDialog",
                        new DialogParameters
                        {
                            { "AlertType", AlertType.Error }, { "Title", "温馨提示" }, { "Message", "请完善证书配置" }
                        },
                        delegate { }
                    );
                    return;
                }

                var cmd =
                    $"-genkey -alias {_keyAlias} -keyalg RSA -keypass {_keyPassword} -keystore {_keySavePath} -storetype JKS -storepass {_keyPassword} -validity 3650 -dname CN={_keySurname},OU={_keyCompany},L={_keyDistrict},ST={KeyProvince},C=CN";
                CommandHub.Get.ExecuteCommand("keytool ", cmd,
                    delegate
                    {
                        RequestClose?.Invoke(new DialogResult(
                            ButtonResult.OK, new DialogParameters
                            {
                                { "KeySavePath", _keySavePath },
                                { "KeyAlias", _keyAlias },
                                { "KeyPassword", _keyPassword }
                            }
                        ));
                    });
            });
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            Title = parameters.GetValue<string>("Title");
        }
    }
}